package com.springboot.fram.core.encryption;

import java.io.UnsupportedEncodingException;

/**
 * @author liheng
 * @ClassName Base64
 * @Description
 * @date 2020-05-08 4:02
 */
public class Base64 {
    private final static java.util.Base64.Encoder encoder = java.util.Base64.getEncoder();
    private final static java.util.Base64.Decoder decoder = java.util.Base64.getDecoder();

    /**
     * Base64编码，byte[] 转 String
     *
     * @param bytes byte[]
     * @return 字符串
     */
    public static String encodeBytesToString(byte[] bytes) {
        return encoder.encodeToString(bytes);
    }

    /**
     * Base64解码，String 转 byte[]
     *
     * @param text 字符串
     * @return byte[]
     */
    public static byte[] decodeStringToBytes(String text) {
        return decoder.decode(text);
    }

    /**
     * 解碼
     *
     * @param decodeText
     * @return
     */
    public static String base64Decode(String decodeText) {
        try {
            return new String(decoder.decode(decodeText), "UTF-8").trim();
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }

    }

    /**
     * 編碼
     *
     * @param encodeText
     * @return
     */
    public static String base64Encode(String encodeText) {
        try {
            return encoder.encodeToString(encodeText.getBytes("UTF-8")).trim();
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }

    }
}
